package com.bsh.erp.tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

public class TokenUtil {  

	/** 
	 *  
	 *  @Description    : 身份验证token值算法： 
	 *                              算法是：将特定的某几个参数一map的数据结构传入， 
	 *                              进行字典序排序以后进行md5加密,32位小写加密； 
	 *  @Method_Name    : authentication 
	 *  @param token        请求传过来的token 
	 *  @param srcData   约定用来计算token的参数 
	 *  @return  
	 */  
	public static String authentication() throws Exception{  
		//排序，根据keyde 字典序排序  
		Map tokenMap = new HashMap<String,Object>();
		Random ramdom = new Random();
		tokenMap.put("time", System.currentTimeMillis());//时间戳
		tokenMap.put("random", RandomUtils.generateString(15));//随机数
		
		if(null == tokenMap){  
			throw new Exception("传入参数为空");  
		}  
		List<Map.Entry<String,Object>> list = new ArrayList<Map.Entry<String,Object>>(tokenMap.entrySet());  
		Collections.sort(list, new Comparator<Map.Entry<String, Object>>(){  
			//升序排序  
			public int compare(Entry<String,Object> o1, Entry<String,Object> o2){  
				return o1.getKey().compareTo(o2.getKey());  
			}  
		});  

		StringBuffer srcSb = new StringBuffer();  
		for(Map.Entry<String , Object>srcAtom : list){  
			srcSb.append(String.valueOf(srcAtom.getValue()));  
		}  
		System.out.println("身份验证加密前字符串："+srcSb.toString());  
		//计算token  
		String token = MD5.MD5Encode(srcSb.toString());  
		System.out.println(token);//for test  
		return token;  
	}  
	/*public static void main(String[] args) {
		try {
			SecurityUtil.authentication();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
}  